<!--头部公共部分-->
{include file="common/header" /}



<script>
    var assignment = parseInt('{$assignment}');
    var campus_id = parseInt('{$campus_id}');
    var payee = parseInt('{$payee}');
    var payment_method = parseInt('{$payment_method}');
    var amount = '{$amount}';
    var remark = '{$remark}';

</script>

<div style="padding:15px;font-size: 14px;">
    <form class="layui-form layui-form-pane">
        <div class="layui-form-item">
            <div class="layui-inline">
                <label class="layui-form-label">订单编号</label>
                <div class="layui-input-inline">
                    <input type="text" name="contract_number" readonly value="{$contract.contract_number}"
                           autocomplete="off" class="layui-input">
                </div>
            </div>
            <div class="layui-inline">
                <label class="layui-form-label">待收款</label>
                <div class="layui-input-inline">
                    <input type="text" readonly value="{$contract.stay_amount}元" autocomplete="off"
                           class="layui-input">
                    <input type="hidden" id="stay_amount" value="{$contract.stay_amount}">
                </div>
            </div>
            <div class="layui-inline">
                <label class="layui-form-label">储值余额</label>
                <div class="layui-input-inline">
                    <input type="text" readonly value="{$contract.storedvalue}元" autocomplete="off"
                           class="layui-input">
                </div>
            </div>
        </div>
        <div class="layui-form-item" style="margin-top: -10px;">
            <div class="layui-inline">
                <label class="layui-form-label"><span class="x-red">*</span>收费归属</label>
                <div class="layui-input-inline">
                    <!--
                    <select name="assignment" lay-verify="required">
                        <option value=""></option>
                        {foreach name="data[24]" id="vo"}
                        <option value="{$vo.id}">{$vo.name}</option>
                        {/foreach}
                    </select>
                    -->
                    <script>
                        document.write('<select name="assignment" lay-verify="required">');
                        document.write('<option value=""></option>');
                        document.write('{foreach name="data[24]" id="vo"}');
                        var id = parseInt("{$vo.id}");
                        if(id === assignment){
                            document.write('<option value="{$vo.id}" selected>{$vo.name}</option>');
                        }else{
                            document.write('<option value="{$vo.id}">{$vo.name}</option>');
                        }
                        document.write('{/foreach}');
                        document.write('</select>');
                    </script>
                </div>
            </div>
            <div class="layui-inline">
                <label class="layui-form-label"><span class="x-red">*</span>收款校区</label>
                <div class="layui-input-inline">
                    <!--
                    <select name="campus_id" lay-verify="required">
                        <option value=""></option>
                        {foreach name="campus" id="vo"}
                        <option value="{$vo.id}">{$vo.campus_name}</option>
                        {/foreach}
                    </select>
                    -->
                    <script>
                        document.write('<select name="campus_id" lay-verify="required">');
                        document.write('<option value=""></option>');
                        document.write('{foreach name="campus" id="vo"}');
                        var id = parseInt("{$vo.id}");
                        if(id === campus_id){
                            document.write('<option value="{$vo.id}" selected>{$vo.campus_name}</option>');
                        }

                        document.write('{/foreach}');
                        document.write('</select>');
                    </script>
                </div>
            </div>
            <div class="layui-inline">
                <label class="layui-form-label"><span class="x-red">*</span>收款人</label>
                <div class="layui-input-inline">

                    <select name="payee" lay-verify="required">
                        <option value="{$Think.session.user.id}">{$Think.session.user.name}</option>
                    </select>

                </div>
            </div>
        </div>
        <fieldset class="layui-elem-field layui-field-title">
            <legend>资金分配<span class="x-red" style="font-size: 14px;">(默认优先分配物品项目)</span></legend>
        </fieldset>
        <div class="layui-input-block">
            {volist name="project_list" id="v"}
            {if $v.project_type == 3}
            <input type="checkbox" name="project" value="{$v['project_id']}" class="layui-input" title="{$v['project_title']}" lay-skin="primary" disabled  checked/>
            {else}
            <input type="checkbox" lay-filter="share_filter" name="project" value="{$v['project_id']}" class="layui-input" title="{$v['project_title']}" lay-skin="primary"  checked/>
            {/if}

            {/volist}
        </div>
        <fieldset class="layui-elem-field layui-field-title">
            <legend>业绩分配</legend>
        </fieldset>
        <div class="layui-tab-content">
            <div class="layui-tab-item layui-show">
                <div class="layui-row">
                    <div class="layui-col-md6">
                            <fieldset class="layui-elem-field layui-field-title" >
                                <legend style="font-size: 14px;">校区业绩分配</legend>
                            </fieldset>


                        <script>
                            var num = '{$num}';
                            for(var i =0; i< num; i++){
                                document.write('<div class="layui-form-item" style="margin-top: -10px;">');
                                document.write('<div class="layui-inline">');
                                document.write('<div class="layui-input-inline" style="width: 150px;font-size: 10px;">')
                                document.write('<select name="campus_id_all"  lay-search="">');
                                document.write('<option value=""></option>');
                                document.write('{foreach name="campus" id="vo"}');
                                document.write('<option value="{$vo.id}">{$vo.campus_name}</option>');
                                document.write('{/foreach}');
                                document.write('</select>');
                                document.write('</div>');
                                document.write('<div class="layui-input-inline" style="width: 150px;font-size: 10px;">');
                                document.write('<select name="assignment_all"  lay-search="">');
                                document.write('<option value=""></option>');
                                document.write('{foreach name="data[24]" id="vo"}');
                                document.write('<option value="{$vo.id}">{$vo.name}</option>');
                                document.write('{/foreach}');
                                document.write('</select>');
                                document.write('</div>');


                                document.write('<div class="layui-input-inline" style="width: 50px;font-size: 10px;">');
                                document.write('<input type="number" id="rate_all"  min="0" max="100" value="100"  name="allocation_rate_all"  autocomplete="off" class="layui-input">');
                                document.write('</div>');
                                document.write('<label class="layui-form-mid layui-word-aux" >%</label>');


                                document.write('<div class="layui-inline" style="margin-left: 15px">');
                                document.write('<a class="layui-form-mid layui-word-aux" id="add_all" onclick="addline(this)"  style="cursor: pointer" title="增加"><i class="layui-icon layui-icon-add-1"></i></a>');
                                document.write('<a class="layui-form-mid layui-word-aux" id="delete_all" onclick="deleteline(this)"  style="cursor: pointer" title="删除"><i class="layui-icon layui-icon-delete"></i></a>');
                                document.write('</div>');

                                document.write('</div>');
                                document.write('</div>');
                            }
                        </script>

                    </div>
                    <div class="layui-col-md6">
                            <fieldset class="layui-elem-field layui-field-title" >
                                <legend style="font-size: 14px;">个人业绩分配</legend>
                            </fieldset>
                        <tbody id="xq_only">
                        <script>
                            var num_only = '{$num_only}';
                            for(var i = 0; i < num_only; i++){
                                document.write('<div class="layui-form-item" style="margin-top: -10px;">');
                                document.write('<div class="layui-inline">');

                                document.write('<div class="layui-input-inline" style="width: 150px;font-size: 10px;">');
                                document.write('<select name="payee_only"  lay-search="">');
                                document.write('<option value=""></option>');
                                document.write('{foreach name="payee_user" id="vo"}');
                                document.write('<option value="{$vo.id}">{$vo.name}</option>');
                                document.write('{/foreach}');
                                document.write('</select>');
                                document.write('</div>');

                                document.write('<div class="layui-input-inline" style="width: 150px;font-size: 10px;">');
                                document.write('<select name="assignment_only"  lay-search="">');
                                document.write('<option value=""></option>');
                                document.write('{foreach name="data[24]" id="vo"}');
                                document.write('<option value="{$vo.id}">{$vo.name}</option>');
                                document.write('{/foreach}');
                                document.write('</select>');
                                document.write('</div>');


                                document.write('<div class="layui-input-inline" style="width: 50px;font-size: 10px;">');
                                document.write('<input type="number" id="rate_only"  min="0" max="100" value="100"  name="allocation_rate_only"  autocomplete="off" class="layui-input">');
                                document.write('</div>');
                                document.write('<label class="layui-form-mid layui-word-aux" >%</label>');



                                document.write('<div class="layui-inline" style="margin-left: 15px">');
                                document.write('<a class="layui-form-mid layui-word-aux" id="add_only" onclick="addlineonly(this)"  style="cursor: pointer" title="增加"><i class="layui-icon layui-icon-add-1"></i></a>');
                                document.write('<a class="layui-form-mid layui-word-aux" id="delete_only" onclick="deletelineonly(this)"  style="cursor: pointer" title="删除"><i class="layui-icon layui-icon-delete"></i></a>');
                                document.write('</div>');


                                document.write('</div>');
                                document.write('</div>');
                            }

                        </script>
                        </tbody>

                        </div>
                    </div>
                </div>
            </div>


        <div class="layui-tab">
            <fieldset class="layui-elem-field layui-field-title">
                <legend>支付通道</legend>
            </fieldset>
            <div class="layui-tab-content">
                <div class="layui-tab-item layui-show">
                    <div class="layui-form-item">
                        <div class="layui-inline">
                            <label class="layui-form-label"><span class="x-red">*</span>支付方式</label>
                            <div class="layui-input-inline">
                                <!--
                                <select name="payment_method" lay-verify="required">
                                    <option selected value=""></option>
                                    {foreach name="data[20]" id="vo"}
                                    <option value="{$vo.id}">{$vo.name}</option>
                                    {/foreach}
                                </select>
                                -->
                                <script>
                                document.write('<select name="payment_method" lay-verify="required">');
                                document.write('<option value=""></option>');
                                document.write('{foreach name="data[20]" id="vo"}');
                                var id = parseInt("{$vo.id}");
                                if(id === payment_method){
                                    document.write('<option value="{$vo.id}" selected>{$vo.name}</option>');
                                }else{
                                    document.write('<option value="{$vo.id}">{$vo.name}</option>');
                                }
                                document.write('{/foreach}');
                                document.write('</select>');
                            </script>

                            </div>
                        </div>
                        <div class="layui-inline">
                            <label class="layui-form-label"><span class="x-red">*</span>收款金额</label>
                            <div class="layui-input-inline">
                                <input type="text" id="amount" name="amount" value="{$amount}" lay-verify="required" placeholder="收款金额" autocomplete="off"
                                       class="layui-input">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <label class="layui-form-label"><span class="x-red">*</span>收款凭证号</label>
                            <div class="layui-input-inline">
                                <input type="text" name="receipt_number" value="{$time}" readonly placeholder="收款凭证号" autocomplete="off"
                                       class="layui-input">
                            </div>
                        </div>
                    </div>
                    <div class="layui-form-item layui-form-text">
                        <label class="layui-form-label">收款备注</label>
                        <div class="layui-input-block">
                            <textarea name="remark" placeholder="收款备注" class="layui-textarea">{$remark}</textarea>
                        </div>
                    </div>
                    <div class="layui-inline">
                        <input type="hidden" name="id" value="{$contract.id}">
                        <input type="hidden" name="user_id" value="{$user_id}">
                        <input type="hidden" name="type" value="2">
                        <input class="layui-btn layui-btn-sm layui-btn-normal" lay-submit lay-filter="form" value="保存" readonly>
                    </div>
                </div>
            </div>
        </div>
    </form>
</div>

<!--底部公共部分-->
{include file="common/footer" /}
<script>


    //添加
    function addline(obj)
    {
        var assignment =  $('select[name=assignment]').val(); //收费归属
        var campus_id = $('select[name=campus_id]').val(); //收款校区
        var payee = $('select[name=payee]').val(); //收款人
        var payment_method = $('select[name=payment_method]').val(); //支付方式
        var amount = $('input[name=amount]').val(); //收款金额
        var remark = $('textarea[name=remark]').val(); //收款备注
        var field = {};
        field.assignment = assignment;
        field.campus_id = campus_id;
        field.payee = payee;
        field.payment_method = payment_method;
        field.amount = amount;
        field.remark = remark;

        $.ajax({
            url: '/admin/contract/additem',
            method: "post",
            data: field,
            beforeSend: function () {

            },
            success: function (result) {
                if (result.code === 1) {
                    location.reload();
                }

            },
            complete: function () {
            },
            error: function (error) {
                console.log(error)
            }
        });

    }

    //删除
    function deleteline(obj)
    {


        var assignment =  $('select[name=assignment]').val(); //收费归属
        var campus_id = $('select[name=campus_id]').val(); //收款校区
        var payee = $('select[name=payee]').val(); //收款人
        var payment_method = $('select[name=payment_method]').val(); //支付方式
        var amount = $('input[name=amount]').val(); //收款金额
        var remark = $('textarea[name=remark]').val(); //收款备注
        var field = {};
        field.assignment = assignment;
        field.campus_id = campus_id;
        field.payee = payee;
        field.payment_method = payment_method;
        field.amount = amount;
        field.remark = remark;

        $.ajax({
            url: '/admin/contract/deleteitem',
            method: "post",
            data: field,
            beforeSend: function () {

            },
            success: function (result) {
                if (result.code === 1) {
                    location.reload();
                }

            },
            complete: function () {
            },
            error: function (error) {
                console.log(error)
            }
        });
    }

    //添加
    function addlineonly(obj)
    {
        var assignment =  $('select[name=assignment]').val(); //收费归属
        var campus_id = $('select[name=campus_id]').val(); //收款校区
        var payee = $('select[name=payee]').val(); //收款人
        var payment_method = $('select[name=payment_method]').val(); //支付方式
        var amount = $('input[name=amount]').val(); //收款金额
        var remark = $('textarea[name=remark]').val(); //收款备注
        var field = {};
        field.assignment = assignment;
        field.campus_id = campus_id;
        field.payee = payee;
        field.payment_method = payment_method;
        field.amount = amount;
        field.remark = remark;
        $.ajax({
            url: '/admin/contract/additemonly',
            method: "post",
            data: field,
            beforeSend: function () {

            },
            success: function (result) {
                if (result.code === 1) {
                    location.reload();
                }

            },
            complete: function () {
            },
            error: function (error) {
                console.log(error)
            }
        });
    }

    //删除
    function deletelineonly(obj)
    {
        var assignment =  $('select[name=assignment]').val(); //收费归属
        var campus_id = $('select[name=campus_id]').val(); //收款校区
        var payee = $('select[name=payee]').val(); //收款人
        var payment_method = $('select[name=payment_method]').val(); //支付方式
        var amount = $('input[name=amount]').val(); //收款金额
        var remark = $('textarea[name=remark]').val(); //收款备注
        var field = {};
        field.assignment = assignment;
        field.campus_id = campus_id;
        field.payee = payee;
        field.payment_method = payment_method;
        field.amount = amount;
        field.remark = remark;
        $.ajax({
            url: '/admin/contract/deleteitemonly',
            method: "post",
            data: field,
            beforeSend: function () {

            },
            success: function (result) {
                if (result.code === 1) {
                    location.reload();
                }

            },
            complete: function () {
            },
            error: function (error) {
                console.log(error)
            }
        });
    }



    layui.use(['element', 'jquery', 'form', 'laydate', 'layer'], function () {
        let $ = layui.$,
            layer = layui.layer,
            form = layui.form,
            element = layui.element,
            laydate = layui.laydate,
            jquery = layui.jquery;

        //日期
        laydate.render({
            elem: '#date'
            ,trigger: 'click'
        });


        //监听提交
        form.on('submit(form)', function (data) {

            var campus_id_alls = document.getElementsByName("campus_id_all"); //校区
            var assignment_alls = document.getElementsByName("assignment_all"); //收费归属
            var allocation_rate_alls = document.getElementsByName("allocation_rate_all"); //校区业绩比例
            var amount_already_all  = $('input[name=amount]').val();  //收款金额

            if(amount_already_all <= 0){
                layer.msg('收款金额不能小于或者等于0');
                return;
            }

            var arr_json_all = [];
            for(var i =0; i < campus_id_alls.length; i ++){
                    if(campus_id_alls[i].value.length > 0 && assignment_alls[i].value.length > 0 && allocation_rate_alls[i].value.length > 0){
                        var row = {};
                        var index= campus_id_alls[i].selectedIndex ;
                        var index_ = assignment_alls[i].selectedIndex
                        row.campus_id_all =  campus_id_alls[i].value;
                        row.campus_id_all_text = campus_id_alls[i].options[index].text;
                        row.assignment_all = assignment_alls[i].value;
                        row.assignment_all_text = assignment_alls[i].options[index_].text;
                        row.rate_all = allocation_rate_alls[i].value;
                        row.rate_all_money =  ((row.rate_all / 100) * amount_already_all).toFixed(2);
                        row.amount_already_all = amount_already_all;
                        arr_json_all.push(row);
                    }
            }
        /*****************************************************************************************/

            var payee_onlys = document.getElementsByName("payee_only"); //分配人
            var assignment_onlys = document.getElementsByName("assignment_only"); //收费归属
            var allocation_rate_onlys = document.getElementsByName("allocation_rate_only"); //校区业绩比例
            var amount_already_only  = $('input[name=amount]').val();  //收款金额

            var arr_json_only = [];
            for(var i =0; i < payee_onlys.length; i ++){
                if(payee_onlys[i].value.length > 0 && assignment_onlys[i].value.length > 0 && allocation_rate_onlys[i].value.length > 0){
                    var row = {};
                    var index= payee_onlys[i].selectedIndex ;
                    var index_ = assignment_onlys[i].selectedIndex
                    row.payee_only =  payee_onlys[i].value;
                    row.payee_only_text = payee_onlys[i].options[index].text;
                    row.assignment_only = assignment_onlys[i].value;
                    row.assignment_only_text = assignment_onlys[i].options[index_].text;
                    row.rate_only = allocation_rate_onlys[i].value;
                    row.rate_only_money =  ((row.rate_only / 100) * amount_already_only).toFixed(2);
                    row.amount_already_only = amount_already_only;
                    arr_json_only.push(row);
                }
            }

            /*
            console.log(JSON.stringify(arr_json_all));
            console.log(JSON.stringify(arr_json_only));
            */

            //发异步，把数据提交给php
            let field = data.field;
            field.arr_json_all =  JSON.stringify(arr_json_all);
            field.arr_json_only = JSON.stringify(arr_json_only);
			field.receivables_status = '{$receivables_status}';
            let stay_amount = $('#stay_amount').val();
            let amount = $('#amount').val();
            let checked_ids = [];
            let input =  document.getElementsByName("project");
            for(var i=0;i<input.length;i++) {
                if (input[i].checked == true) {
                    checked_ids.push(input[i].value);
                }
            }
            let checked_ids_ = checked_ids.join(',');
            field.checked_ids = checked_ids_;

            /*
            if(Number(amount) > Number(stay_amount)){
                layer.msg('收款金额不能大于总金额，请重新输入');
                $('#amount').val('');
                return false;
            }
            */
            let isLoad;
            $.ajax({
                url: '{:url("contract/receivables")}',
                method: "post",
                data: field,
                success: function (result) {
                    layer.msg(result.msg);
                    if (result.code === 1 && result.data === 0) {
                        layer.open({
                            type: 2,
                            skin: 'layui-layer-lan',
                            closeBtn: 2,
                            title: '指派教务',
                            // btn: ['确定', '取消'],
                            area: ['90%', '90%'],
                            shadeClose: false,
                            content: "{:url('Contract/eduadmin')}?campus_id="+campus_id + '&contract_id=' + field.id,
                            end:function () {
                                if(field.payment_method == '79'){
                                    let index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
                                    parent.layer.close(index); //再执行关闭
                                }else{
                                    field.mch_id = 1
                                    field.checked_ids = result.data
                                    $.ajax({
                                        url: result.url,
                                        method: "post",
                                        data: field,
                                        success: function (res) {
                                            layer.msg(res.msg);
                                            console.log(res);
                                            if (res.code === 1) {
                                                let url = '/api/Wxpay/payflow/?img='+res.data.url;
                                                layer.open({
                                                    type: 2,
                                                    skin: 'layui-layer-lan',
                                                    closeBtn: 2,
                                                    title: '收款',
                                                    area: ['30%', '50%'],
                                                    shadeClose: false,
                                                    content: url,
                                                    end:function () {
                                                        let index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
                                                        parent.layer.close(index); //再执行关闭
                                                    }
                                                });
                                            }
                                        },
                                        complete: function () {
                                            layer.close(isLoad)
                                        },
                                        error: function (error) {
                                            console.log(error)
                                        }
                                    });
                                }
                            }
                        });

                    }else if(result.code === 1 && result.data !== 0){
                        if(field.payment_method == '79'){
                            let index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
                            parent.layer.close(index); //再执行关闭
                        }else{
                            field.mch_id = 1
                            field.checked_ids = result.data
                            $.ajax({
                                url: result.url,
                                method: "post",
                                data: field,
                                success: function (res) {
                                    layer.msg(res.msg);
                                    console.log(res);
                                    if (res.code === 1) {
                                        let url = '/api/Wxpay/payflow/?img='+res.data.url;
                                        layer.open({
                                            type: 2,
                                            skin: 'layui-layer-lan',
                                            closeBtn: 2,
                                            title: '收款',
                                            area: ['30%', '50%'],
                                            shadeClose: false,
                                            content: url,
                                            end:function () {
                                                let index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
                                                parent.layer.close(index); //再执行关闭
                                            }
                                        });
                                    }
                                },
                                complete: function () {
                                    layer.close(isLoad)
                                },
                                error: function (error) {
                                    console.log(error)
                                }
                            });
                        }
                    }
                },
                complete: function () {
                    layer.close(isLoad)
                },
                error: function (error) {
                    console.log(error)
                }
            });
            return false;
        });
    });
</script>
